package cn.com.gridinfo.sooc.core.expand.callname.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.marshalchen.ultimaterecyclerview.UltimateRecyclerviewViewHolder;
import com.marshalchen.ultimaterecyclerview.UltimateViewAdapter;

import java.util.List;

import cn.com.gridinfo.sooc.R;
import cn.com.gridinfo.sooc.core.expand.callname.activity.SignInfoActivity;
import cn.com.gridinfo.sooc.core.expand.callname.bean.ClassInfo;

/**
 * 今日点名界面列表适配器
 *
 * @author yingchunli
 */
public class TodayCallnameAdapter extends UltimateViewAdapter<TodayCallnameAdapter.ViewHolder> {

    public List<ClassInfo> classInfoList;
    Context context;

    public TodayCallnameAdapter(List<ClassInfo> classInfoList, Context context) {
        this.classInfoList = classInfoList;
        this.context = context;
    }

    @Override
    public RecyclerView.ViewHolder onCreateHeaderViewHolder(ViewGroup parent) {
        return null;
    }

    @Override
    public void onBindHeaderViewHolder(RecyclerView.ViewHolder holder, int position) {

    }

    @Override
    public ViewHolder getViewHolder(View view) {
        return new ViewHolder(view, false);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent) {
        //加载布局
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.expand_callname_todaycallname_item, parent, false);
        ViewHolder holder = new ViewHolder(view, true);
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        if (position < getItemCount() &&
                (customHeaderView != null ? position <= classInfoList.size() : position < classInfoList.size())
                && (customHeaderView != null ? position > 0 : true)) {
            final ClassInfo classInfo = classInfoList.get(position);
            holder.ktbh.setText("" + classInfo.getKtbh());
            holder.kcmc.setText(classInfo.getKcmc());
            holder.ktzt.setText(classInfo.getKtzt());
            holder.qdrs.setText("实到人数：" + classInfo.getQdrs());
            if (((Integer) classInfo.getPzqd()) == 0) {
                //不需要拍照签到
                holder.pzqd.setVisibility(View.GONE);
            } else {
                //拍照签到
                holder.pzqd.setVisibility(View.VISIBLE);
            }
            if (classInfo.getSkdd().isEmpty()) {
                //没有上课地点
                holder.skdd.setVisibility(View.GONE);
            } else {
                //有上课地点
                holder.skdd.setText(classInfo.getSkdd());
            }
            //查看签到的点击
            holder.looksign.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnItemClickLitener.OnItemClick(classInfoList.get(position));
                }
            });
            //布局上半部分点击，可以查看签到
            holder.above.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    //跳转到签到详情界面
                    mOnItemClickLitener.OnItemClick(classInfoList.get(position));
                }
            });
            //设置签到开关的状态
            if (classInfo.getQdzt().equals("0")) {
                holder.qdkg.setChecked(false);
            } else {
                holder.qdkg.setChecked(true);
            }
            //签到开关的控制
            holder.qdkg.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnItemClickLitener.OnItemClick(v, classInfoList.get(position));
                }
            });
        }
    }

    @Override
    public int getAdapterItemCount() {
        return classInfoList.size();
    }

    @Override
    public long generateHeaderId(int position) {
        return position;
    }

    /**
     * 点击事件接口
     */
    public interface OnItemClickListener {
        void OnItemClick(View view, ClassInfo classInfo);

        void OnItemClick(ClassInfo classInfo);
    }

    private OnItemClickListener mOnItemClickLitener;

    public void setmOnItemClickLitener(OnItemClickListener mOnItemClickLitener) {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }

    //加载更多
    public void loadmore(List<ClassInfo> list) {
        classInfoList.addAll(list);
        notifyDataSetChanged();
    }

    //刷新列表
    public void refresh(List<ClassInfo> list) {
        classInfoList.clear();
        classInfoList.addAll(list);
        notifyDataSetChanged();
    }

    //ViewHolder
    public class ViewHolder extends UltimateRecyclerviewViewHolder {
        TextView ktbh, kcmc, ktzt, qdrs, pzqd, skdd, looksign;
        CheckBox qdkg;
        RelativeLayout above;//布局的上面区域

        public ViewHolder(View itemView, boolean isItem) {
            super(itemView);
            if (isItem) {
                above = (RelativeLayout) itemView.findViewById(R.id.rl);
                ktbh = (TextView) itemView.findViewById(R.id.course_number);//课堂编号
                kcmc = (TextView) itemView.findViewById(R.id.kcmc_Todaycallname);//课程名称
                ktzt = (TextView) itemView.findViewById(R.id.ktzt_Todaycallname);//课堂主题
                qdrs = (TextView) itemView.findViewById(R.id.qdrs_Todaycallname);//签到人数
                pzqd = (TextView) itemView.findViewById(R.id.pzqd_Todaycallname);//拍照签到
                skdd = (TextView) itemView.findViewById(R.id.skdd_todaycallname_item);//上课地点
                qdkg = (CheckBox) itemView.findViewById(R.id.qdIcon_todayName);//签到开关
                looksign = (TextView) itemView.findViewById(R.id.looksign_Todaycallname);//查看签到
            }
        }


    }
}
